define(function(require,exports,module) {
    var myPub = require('./public');
    var oWrap = $('.js_wrap');
    var oSelectDateS = $('.js_downTimeArea_left');          // 起始时间
    var oSelectDateE = $('.js_downTimeArea_right');         // 结束时间
    var oSelectOrderType = $('.js_orderStateArea');         // 选择类型区域
    var oDownLoadBtn = $('.js_downLoadBtn');                // 立即下载按钮
    var oWdateLeftInput = $('.js_WdateLeft');               // 左侧日期输入框
    var oWdateRightInput = $('.js_WdateRight');             // 右侧日期输入框
    var oToWdateLeft = $('.js_toWdateLeft');                // 左边日历
    var oToWdateRight = $('.js_toWdateRight');              // 右边日历

    // 判断是设备
    userAgent();
    function userAgent() {
        /*var userAgent = navigator.userAgent;
        // 如果不是在电脑端
        if(userAgent.indexOf('Windows') === -1){

            // 弹窗提示
            myPub.TipLayer('请在电脑上打开','no',function () {
                $('.layermbox').addClass('disbutor');
            });
            return

        }else if(userAgent.indexOf('Windows') > -1){ */ // 如果是电脑端
            oToWdateLeft.on('click',function () {
                oWdateLeftInput.focus();
             });
            oToWdateRight.on('click',function () {
                oWdateRightInput.focus();
            });
            oWdateLeftInput.on('focus',function () {
                test(oWdateLeftInput,WdateLeft)
            });
            oWdateRightInput.on('focus',function () {
                test(oWdateRightInput,WdateRight)
            });
            function test(obj,el) {
                WdatePicker({
                    dateFmt:'yyyy/MM/dd',
                    vel:el,
                    maxDate:'%y-%M-%d'
                });
                var nWrapLeft = oWrap.offset().left;
                var nInputLeft = obj.offset().left;
                var nPickerLeft = nInputLeft - nWrapLeft;

                    $('.mydatePicker').stop().animate({
                        'left': nPickerLeft
                    },150, function () {
                        $('.mydatePicker').css({
                            'opacity':1
                        })
                    });
            }

            // 订单状态选择
            SeletOrderType(oSelectOrderType);

            // 点击立即下载按钮
            oDownLoadBtn.on('click',function () {

                // 判断开始、结束下载时间是否选择
                var oStartTime = oSelectDateS.find('.js_inputPickerday').val();
                var oEndtTime = oSelectDateE.find('.js_inputPickerday').val();
                if(oStartTime == '' || oEndtTime == ''){
                    myPub.TipLayer('请正确选择下载时间','no',function () {
                        $('.layermbox').addClass('disbutor');
                    });
                    return
                }else{

                    // 判断结束下载时间是否大于开始下载时间
                    var nStartNum = parseFloat(oStartTime.replace(/-/g,''));
                    var nEndNum = parseFloat(oEndtTime.replace(/-/g,''));
                    if((nEndNum-nStartNum) < 0){
                        myPub.TipLayer('时间段选择错误','no',function () {
                            $('.layermbox').addClass('disbutor');
                        });
                        return
                    }
                }

                // 在这里写交互
                exportUtil({
                    exportUrl : "/super/export",
                    extraParam : $("#js_distributor_downLoad").HForm('form2json')
                })

            })
        /*}*/
    }


    // 选择订单状态
    function SeletOrderType(obj, fn) {
        (function($$) {
            $$.init();
            var picker = new $$.PopPicker();
            picker.setData([
                {
                    value: "",
                    text: "全部"
                },
                {
                    value: "1",
                    text: "待发货"
                },
                {
                    value: "2",
                    text: "待收货"
                },
                {
                    value: "3",
                    text: "交易完成"
                }
            ]);
            obj[0].addEventListener('click', function() {
                picker.show(function(rs) {
                    obj.find('.js_orderTypeTxt').text(rs[0].text);
                    obj.find('.js_orderStatus').val(rs[0].value);
                    /*
                     * 返回 false 可以阻止选择框的关闭
                     * return false;
                     */

                    if(fn)fn(rs.text)
                });
            }, false);

        })(mui);
    }


    function exportUtil(P) {
        if (P == null) {
            P = {};
        }
        if (P.exportUrl == null){
            return;
        }
        var sequenceId = Math.round(Math.random()*100000);
        var exportForm = $("<form>");
        exportForm.attr("style","display:none");
        exportForm.attr("method","POST");
        exportForm.attr("action",P.exportUrl);
        if(P.extraParam != null){
            if(Array.isArray(P.extraParam)){
                for (var key in P.extraParam){
                    var exData = P.extraParam[key];
                    if(exData.name && exData.value && exData.value != ""){
                        var input = $("<input>");
                        input.attr("type","hidden");
                        input.attr("name", exData.name);
                        input.attr("value",exData.value);
                        exportForm.append(input);
                    }
                }
            }else{
                $.each(P.extraParam,function (name, value) {
                    var input = $("<input>");
                    input.attr("type","hidden");
                    input.attr("name", name);
                    input.attr("value",value);
                    exportForm.append(input);
                })
            }
        }
        var input = $("<input>");
        input.attr("type","hidden");
        input.attr("name", "export");
        input.attr("value","export");
        exportForm.append(input);
        var input2 = $("<input>");
        input2.attr("type","hidden");
        input2.attr("name", "sequenceId");
        input2.attr("value",sequenceId);
        exportForm.append(input2);
        $(document.body).append(exportForm);


        exportForm.submit();

    }

    function updateProcess(queryUrl,sequenceId){
        $.ajax({
            type: 'POST',
            url : queryUrl,
            data : {"export": "getProcess","sequenceId":sequenceId},
            success: function (data) {
                if(data.status == "SUCCESS"){
                    var process = Number(data.message);
                    $("#pbar").omProgressbar("value", process);
                    /*$("#pbar").omProgressbar("text", '已完成{process}%');*/
                    // 定时更新进度条
                    if(process < 100){
                        setTimeout(function() {
                            updateProcess(queryUrl,sequenceId);
                        }, 500);
                    }else{
                        /*autoCloseProcess(3);*/
                        myPub.TipLayer('导出成功','no',function () {
                            $('.layermbox').addClass('disbutor');
                        });
                    }
                }
            }
        });
    }

    function autoCloseProcess(remainSeconds) {
        if(remainSeconds <= 0){
            $('#exportForm').omDialog('close');
            $('#exportForm').remove();
            return;
        }
        $('#exprotMsg').html("导出完成,"+remainSeconds+"秒后关闭该对话框");
        setTimeout(function() {
            autoCloseProcess(remainSeconds-1);
        }, 1000);
    }

});